/*
 * Copyright (c) 2013-2014, starteasy Inc. All Rights Reserved.
 * 
 * Project Name: sample
 * $Id:  2016-11-14 17:11:45 $ 
 */
package cn.starteasy.sample.web.controller.api;

import cn.starteasy.core.common.domain.persistent.SearchEnum;
import cn.starteasy.core.common.domain.persistent.utils.ConditionBuilder;
import cn.starteasy.core.common.domain.wrapper.BooleanWrapper;
import cn.starteasy.sample.service.IStreetService;
import cn.starteasy.core.common.domain.view.admin.SearchField;
import cn.starteasy.core.common.domain.wrapper.StringWrapper;
import cn.starteasy.core.common.domain.view.BizData4Page;
import cn.starteasy.core.common.exception.BizException;
import cn.starteasy.core.common.protocol.utils.RtnCodeEnum;

import cn.starteasy.sample.domain.Street;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value="/api")
public class ApiStreetController{
    private static final Logger logger = LoggerFactory.getLogger(ApiStreetController.class);

    @Autowired
    private IStreetService streetService;

   /**
     * 新增 街道
     * @param street
     * @return  处理条数
     */
    @ResponseBody
    @RequestMapping(value = "/street", method = RequestMethod.POST)
    public Object addStreet(@RequestBody Street street) {
        try {
            String msg = "";

            if(street==null) {
                msg = "添加街道参数不正确";
            }else if(street.getStatus() != null){
                msg = "街道状态不能为空";
            }else if(street.getCreator() != null){
                msg = "街道创建人不能为空";
            }else if(street.getCreateDate() != null){
                msg = "街道创建时间不能为空";
            }else if(street.getLastModifier() != null){
                msg = "街道修改人不能为空";
            }else if(street.getLastModDate() != null){
                msg = "街道修改时间不能为空";
            }

            if(StringUtils.isNotBlank(msg)){
                throw new BizException(RtnCodeEnum.UNKNOW.getValue(), msg);
            }

            streetService.create(street);
            return new BooleanWrapper(true);
        } catch (BizException bize) {
            throw bize;
        } catch (Exception e) {
            logger.error(e.getMessage());
            throw new BizException(RtnCodeEnum.UNKNOW.getValue(), "新增街道异常");
        }
    }


    /**
     * 删除 街道
     *
     * @param {streetId} 街道ID
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/street/{streetId}" ,method = RequestMethod.DELETE)
    public Object delStreet(@PathVariable String streetId) {
        try {
            if(StringUtils.isBlank(streetId)){
                throw new BizException(RtnCodeEnum.UNKNOW.getValue(), "删除街道失败，参数不正确");
            }
            streetService.deleteByCondition(ConditionBuilder.condition("id", SearchEnum.eq, streetId));
            return new BooleanWrapper(true);
        } catch (BizException bize) {
            throw bize;
        } catch (Exception e) {
            logger.error(e.getMessage());
            throw new BizException(RtnCodeEnum.UNKNOW.getValue(), "删除街道异常");
        }
    }


    /**
     * 修改街道数据
     * @param street 提交上来的街道JSON数据
     * @param streetId  街道ID
     * @return  修改条数
     */
    @ResponseBody
    @RequestMapping(value = "/street/{streetId}", method = RequestMethod.PATCH)
    public StringWrapper editStreet(@RequestBody Street street, @PathVariable String streetId) {
        try {
            String msg = "";
            if(street==null) {
                msg = "添加街道参数不正确";
            }else if(street.getStatus() != null){
                msg = "街道状态不能为空";
            }else if(street.getCreator() != null){
                msg = "街道创建人不能为空";
            }else if(street.getCreateDate() != null){
                msg = "街道创建时间不能为空";
            }else if(street.getLastModifier() != null){
                msg = "街道修改人不能为空";
            }else if(street.getLastModDate() != null){
                msg = "街道修改时间不能为空";

            }

            if(StringUtils.isNotBlank(msg)){
                throw new BizException(RtnCodeEnum.UNKNOW.getValue(), msg);
            }

            Street street_old = (Street) streetService.view(street.getId());
            if(street==null) {
                throw new BizException(RtnCodeEnum.UNKNOW.getValue(), "修改失败，系统未找到相关数据");
            }

            if(street.getId() != null){
                street_old.setId(street.getId());
            }
            if(!StringUtils.isBlank(street.getName())){
                street_old.setName(street.getName());
            }
            if(!StringUtils.isBlank(street.getNumber())){
                street_old.setNumber(street.getNumber());
            }
            if(!StringUtils.isBlank(street.getXpoint())){
                street_old.setXpoint(street.getXpoint());
            }
            if(!StringUtils.isBlank(street.getYpoint())){
                street_old.setYpoint(street.getYpoint());
            }
            if(!StringUtils.isBlank(street.getTownId())){
                street_old.setTownId(street.getTownId());
            }
            if(street.getStatus() != null){
                street_old.setStatus(street.getStatus());
            }
            if(street.getCreator() != null){
                street_old.setCreator(street.getCreator());
            }
            if(street.getCreateDate() != null){
                street_old.setCreateDate(street.getCreateDate());
            }
            if(street.getLastModifier() != null){
                street_old.setLastModifier(street.getLastModifier());
            }
            if(street.getLastModDate() != null){
                street_old.setLastModDate(street.getLastModDate());
            }


            int row=streetService.edit(street_old);
            if(row > 0) {
                return new StringWrapper("修改街道成功");
            }else{
                throw new BizException(RtnCodeEnum.UNKNOW.getValue(), "修改街道失败");
            }
        } catch (BizException bize) {
            throw bize;
        } catch (Exception e) {
            logger.error(e.getMessage());
            throw new BizException(RtnCodeEnum.UNKNOW.getValue(), "修改街道数据异常");
        }
    }


    /**
     * 获取单街道实体
     *
     * @param streetId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/street/{streetId}", method = RequestMethod.GET)
    public Street getStreet(@PathVariable String streetId) {
        try {
            if(StringUtils.isBlank(streetId)){
                throw new BizException(RtnCodeEnum.UNKNOW.getValue(), "参数不正确！");
            }
            Map<String,Object> whereParams = new HashMap<String, Object>();
            whereParams.put("id", streetId);
            Street street= (Street) streetService.viewOne(null, whereParams, null);
            if(null == street){
                throw new BizException(RtnCodeEnum.UNKNOW.getValue(), "系统未找到街道相关数据！");
            }
            return street;
        } catch (BizException bize) {
            throw bize;
        } catch (Exception e) {
            logger.error(e.getMessage());
            throw new BizException(RtnCodeEnum.UNKNOW.getValue(), "获取单街道实体异常");
        }
    }


    /**
     * 街道数据输出 带分页
     *
     * @param street 过滤条件
     * @param page      第几页
     * @return 业务数据列表实体，最终转换为json数据输出
     * @throws ServletException
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping(value = "/streetlist", method = RequestMethod.POST)
    public BizData4Page streetlist(Street street, Integer page) {
        try {
            Map<String, Object> whereParams = new HashMap<String, Object>();
            if(street.getId() != null){
                whereParams.put("id", new SearchField("id", "=", street.getId()));
            }
            if(!StringUtils.isBlank(street.getName())){
                whereParams.put("name", new SearchField("name", "like", "%" + street.getName() + "%"));
            }
            if(!StringUtils.isBlank(street.getNumber())){
                whereParams.put("number", new SearchField("number", "like", "%" + street.getNumber() + "%"));
            }
            if(!StringUtils.isBlank(street.getXpoint())){
                whereParams.put("xpoint", new SearchField("xpoint", "like", "%" + street.getXpoint() + "%"));
            }
            if(!StringUtils.isBlank(street.getYpoint())){
                whereParams.put("ypoint", new SearchField("ypoint", "like", "%" + street.getYpoint() + "%"));
            }
            if(!StringUtils.isBlank(street.getTownId())){
                whereParams.put("townId", new SearchField("townId", "like", "%" + street.getTownId() + "%"));
            }
            if(street.getStatus() != null){
                whereParams.put("status", new SearchField("status", "=", street.getStatus()));
            }
            if(street.getCreator() != null){
                whereParams.put("creator", new SearchField("creator", "=", street.getCreator()));
            }
            if(street.getCreateDate() != null){
                whereParams.put("createDate", new SearchField("createDate", "=", street.getCreateDate()));
            }
            if(street.getLastModifier() != null){
                whereParams.put("lastModifier", new SearchField("lastModifier", "=", street.getLastModifier()));
            }
            if(street.getLastModDate() != null){
                whereParams.put("lastModDate", new SearchField("lastModDate", "=", street.getLastModDate()));
            }


            //other props filter
            whereParams.put("groupOp", "and");

        return streetService.queryPage(null, whereParams, page, 10, 10, null);
        } catch (BizException bize) {
            throw bize;
        } catch (Exception e) {
            logger.error(e.getMessage());
            throw new BizException(RtnCodeEnum.UNKNOW.getValue(), "查询街道数据异常");
        }
    }
}
